<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1 id="timer">倒计时: 5</h1>
    <h1 id="date"></h1>

    <script>
        let time = 5;
        let intervalId = setInterval(() => {
            time--;
            timer.innerText = "倒计时: " + time;
            if (time <= 0) {
                clearInterval(intervalId);
            }
        }, 1000)



        setInterval(() => {
            date.innerText = getDate("-")
        }, 1000);



        function getDate(join, isTime = true) {

            const dt = new Date();

            const y = dt.getFullYear();
            const m = String(dt.getMonth() + 1).padStart(2, "0");
            // 1. 数据 + ""
            const d = (dt.getDate() + "").padStart(2, "0");

            // 2. 数据.toString()
            const hh = dt.getHours().toString().padStart(2, "0");
            // 3. String(数据)
            const mm = String(dt.getMinutes()).padStart(2, "0");
            const ss = dt.getSeconds();


            const date = [y, m, d].join(join);
            const time = [hh, mm, ss].join(':');

            if (isTime) {
                return date + ' ' + time;
            }

            return date;

        }
    </script>
</body>

</html>